package cn.itcast.daggerdemo;

import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import java.io.IOException;

import javax.inject.Inject;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import retrofit2.Retrofit;

public class MainActivity extends AppCompatActivity {

    private TextView mTextContent;

    @Inject
    DaggerPresenter daggerPresenter;
    @Inject
    Retrofit retrofit;
    @Inject
    OkHttpClient okHttpClient;

    private BaseResponse response;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextContent = ((TextView) findViewById(R.id.textview_content));

        AppComponent appComponent = ((MyApplication) getApplication()).getAppComponent();

        DaggerActivityComponent.builder()
                .appComponent(appComponent)
                .activityModule(new ActivityModule(this))
                .build().inject(this);

        response = retrofit.create(BaseResponse.class);

    }

    public void getOrderInfo(View view) {

        final retrofit2.Call<OrderDetailInfo> orderDetailInfo = response.getOrderDetailInfo("123450", "502920");

        orderDetailInfo.clone().enqueue(new retrofit2.Callback<OrderDetailInfo>() {
            @Override
            public void onResponse(retrofit2.Call<OrderDetailInfo> call, retrofit2.Response<OrderDetailInfo> response) {

                OrderDetailInfo orderDetailInfo = response.body();
                String string = orderDetailInfo.toString();

                mTextContent.setText(string);
            }

            @Override
            public void onFailure(retrofit2.Call<OrderDetailInfo> call, Throwable t) {

            }
        });


        //okHttpRequest();

    }

    private void okHttpRequest() {
        Uri buildUri = Uri.parse("http://192.168.209.1:8080/market/orderdetail")
                .buildUpon()
                .appendQueryParameter("orderId", "502920")

               /* .appendQueryParameter("type", "1")
                .appendQueryParameter("page", "0")
                .appendQueryParameter("pageNum", "10")*/


                /*.appendQueryParameter("sku", "1:3:1,2,3,4|2:2:2,3")
                .appendQueryParameter("addressId", "139")
                .appendQueryParameter("paymentType", "1")
                .appendQueryParameter("deliveryType", "1")
                .appendQueryParameter("invoiceType", "1")
                .appendQueryParameter("invoiceTitle", "传智慧播客教育科技有限公司")
                .appendQueryParameter("invoiceContent", "1")*/
                .build();
        Request.Builder request = new Request.Builder();
        request.url(buildUri.toString())
                .addHeader("userid", "123450")
                .get();
        okHttpClient.newCall(request.build()).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

                Log.d("MainActivity", e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                ResponseBody responseBody = response.body();
                String string = responseBody.string();
                Log.i("MainActivity", string);
            }
        });
    }

    public <Data> void setContentData(Data data) {

        mTextContent.setText(data.toString());
    }
}
